package com.javaweb.admin.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.javaweb.admin.entity.AnnualBudget;
import com.javaweb.admin.entity.C37InfRkScBillNew;
import com.javaweb.admin.entity.NcTaskLog;
import com.javaweb.admin.entity.Scheduled;
import com.javaweb.admin.mapper.ScheduledMapper;
import com.javaweb.admin.service.AbstractJob;
import com.javaweb.admin.service.IAnnualBudgetService;
import com.javaweb.admin.service.IBipServiceLogService;
import com.javaweb.admin.service.INcTaskLogService;
import com.javaweb.common.utils.DateUtils;
import com.javaweb.common.utils.JsonResult;
import com.sphchina.esb.webservice.ISendToReviewLog;
import com.sphsine.bip.AnnualBudgetData;
import com.sphsine.bip.AnnualBudgetList;
import com.sphsine.bip.IBIPService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Component
public class UploadAnnualBudgetJob extends AbstractJob {

    @Autowired
    INcTaskLogService iNcTaskLogService;
    @Autowired
    IAnnualBudgetService iAnnualBudgetService;
    @Autowired
    ScheduledMapper scheduledMapper;
    @Autowired
    IBIPService ibipService;
    @Autowired
    IBipServiceLogService bipServiceLogService;


    @Override
    public void execute() {

        String billNo = "";
        String body = "";
        List<AnnualBudget> list= iAnnualBudgetService.GetWSCList();
        logger.info(DateUtils.dateTimeNow() + "获取了" + list.size() + "个年度预算记录");
        NcTaskLog taskLog = new NcTaskLog();
        taskLog.setTaskType("年度预算");
        taskLog.setBillCount(list.size());
        taskLog.setStartTime(DateUtils.now());
        List<AnnualBudgetData> budgetList = new ArrayList<>(1);
        AnnualBudgetList postList = new AnnualBudgetList();
        Integer id;
        if(list.size()>0) {
            AnnualBudget budget;
            AnnualBudgetData data;
            for (int i = 0; i < list.size(); i++) {
                id = list.get(i).getId();
                ibipService.sendBudget("",id);
//                djhs[0] = String.valueOf(list.get(i).getId());
//                budget = iAnnualBudgetService.GetDetail(id);
//                data = new AnnualBudgetData();
//                data.setYearly(budget.getYearly());
//                data.setFactoryOrg(budget.getSaleCode());
//                data.setPurchaseOrg(budget.getPurchaseCode());
//                data.setMaterial(budget.getMaterialCode());
//                data.setInternalPrice(budget.getInternalPrice()==null? BigDecimal.ZERO:budget.getInternalPrice());
//                data.setPurchaseNum(budget.getPurchaseNum());
//                data.setPurchaseAmount(budget.getPurchaseAmount());
//                data.setPurchaseMargin(budget.getPurchaseMargin());
//                data.setSalesUnitPrice(budget.getSalesUnitPrice()==null?BigDecimal.ZERO:budget.getSalesUnitPrice());
//                data.setSaleNum(budget.getSaleNum());
//                data.setSaleAmount(budget.getSaleAmount());
//                data.setSaleMargin(budget.getSaleMargin());
//                data.setVariableCost(budget.getVariableCost());
//                data.setBudgetState("0");
//                data.setImportMark("BS-"+DateUtils.getDate());
//
//
//
//                budgetList.add(data);
//                postList.setData(budgetList);
//
//                billNo=list.get(i).getMaterialCode();
//                body = JSON.toJSONString(postList, SerializerFeature.WriteBigDecimalAsPlain);
//                JsonResult result = ibipService.sendBudget(body, id);
//                budgetList.clear();
                //logger.info(DateUtils.dateTimeNow() + "提交了采购入库单：" + djhs[0]+",处理结果:"+result.getData().toString());
                logger.info(DateUtils.dateTimeNow() + "提交了年度预算：" + id.toString());

            }
        }
        taskLog.setBillNo("");
        taskLog.setEndTime(DateUtils.now());
        iNcTaskLogService.save(taskLog);
        //System.out.println(DateUtils.dateTimeNow()+":上传了一条数据");
        //this.cancel();
    }

    @Override
    public String getName() {
        QueryWrapper<Scheduled> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("classpath",this.getClass().getName());
        Scheduled entity = scheduledMapper.selectOne(queryWrapper);
        return entity.getName();
    }
}
